﻿# 检查并解压googletest-1.17.0.zip
Write-Host "检查并解压googletest-1.17.0.zip..."
if (-not (Test-Path -Path "googletest-1.17.0")) {
    Expand-Archive -Path "googletest-1.17.0.zip" -DestinationPath "." -Force
}

# 进入解压后的目录
Write-Host "进入解压后的目录..."
Set-Location -Path "googletest-1.17.0"

# 创建输出目录结构
Write-Host "创建输出目录结构..."
$outputDirs = @(
    "..\lib",
    "..\lib\x64",
    "..\lib\Win32",
    "..\lib\x64\Debug",
    "..\lib\x64\Release",
    "..\lib\Win32\Debug",
    "..\lib\Win32\Release",
    "..\include"
)

foreach ($dir in $outputDirs) {
    if (-not (Test-Path -Path $dir)) {
        New-Item -Path $dir -ItemType Directory | Out-Null
    }
}

# 清理并重建头文件（删除旧文件）
Write-Host "清理并重建头文件（删除旧文件）..."
$gtestIncludePath = "..\include\googletest"
if (Test-Path -Path $gtestIncludePath) {
    Remove-Item -Path "$gtestIncludePath\*" -Recurse -Force -ErrorAction SilentlyContinue
}

# 构建x64 Debug版本
Write-Host "构建x64 Debug版本..."
$buildDir = "build64d"
New-Item -ItemType Directory -Path "$buildDir" -ErrorAction SilentlyContinue | Out-Null
Set-Location $buildDir
$stagingDir = ".\dist"
cmake -G "Visual Studio 17 2022" -A x64 `
    -DCMAKE_BUILD_TYPE=Debug `
    -DBUILD_SHARED_LIBS=ON `
    -DCMAKE_INSTALL_PREFIX="$stagingDir" `
    ..
cmake --build . --config Debug
cmake --install . --config Debug
Copy-Item -Path "$stagingDir\lib\*.lib" -Destination "..\..\lib\x64\Debug" -Force
Copy-Item -Path "$stagingDir\bin\*.dll" -Destination "..\..\lib\x64\Debug" -Force
robocopy "$stagingDir\include\gmock" "..\..\include\gmock" /E /COPYALL /R:1 /W:1 /NFL /NDL
robocopy "$stagingDir\include\gtest" "..\..\include\gtest" /E /COPYALL /R:1 /W:1 /NFL /NDL
Set-Location -Path ".."

# 构建x64 Release版本
Write-Host "构建x64 Release版本..."
$buildDir = "build64r"
New-Item -ItemType Directory -Path "$buildDir" -ErrorAction SilentlyContinue | Out-Null
Set-Location $buildDir
$stagingDir = ".\dist"
cmake -G "Visual Studio 17 2022" -A x64 `
    -DCMAKE_BUILD_TYPE=Release `
    -DBUILD_SHARED_LIBS=ON `
    -DCMAKE_INSTALL_PREFIX="$stagingDir" `
    ..
cmake --build . --config Release
cmake --install . --config Release
Copy-Item -Path "$stagingDir\lib\*.lib" -Destination "..\..\lib\x64\Release" -Force
Copy-Item -Path "$stagingDir\bin\*.dll" -Destination "..\..\lib\x64\Release" -Force
Set-Location -Path ".."

# 构建Win32 Debug版本
Write-Host "构建Win32 Debug版本..."
$buildDir = "build32d"
New-Item -ItemType Directory -Path "$buildDir" -ErrorAction SilentlyContinue | Out-Null
Set-Location $buildDir
$stagingDir = ".\dist"
cmake -G "Visual Studio 17 2022" -A Win32 `
    -DCMAKE_BUILD_TYPE=Debug `
    -DBUILD_SHARED_LIBS=ON `
    -DCMAKE_INSTALL_PREFIX="$stagingDir" `
    ..
cmake --build . --config Debug
cmake --install . --config Debug
Copy-Item -Path "$stagingDir\lib\*.lib" -Destination "..\..\lib\Win32\Debug" -Force
Copy-Item -Path "$stagingDir\bin\*.dll" -Destination "..\..\lib\Win32\Debug" -Force
Set-Location -Path ".."

# 构建Win32 Release版本
Write-Host "构建Win32 Release版本..."
$buildDir = "build32r"
New-Item -ItemType Directory -Path "$buildDir" -ErrorAction SilentlyContinue | Out-Null
Set-Location $buildDir
$stagingDir = ".\dist"
cmake -G "Visual Studio 17 2022" -A Win32 `
    -DCMAKE_BUILD_TYPE=Release `
    -DBUILD_SHARED_LIBS=ON `
    -DCMAKE_INSTALL_PREFIX="$stagingDir" `
    ..
cmake --build . --config Release
cmake --install . --config Release
Copy-Item -Path "$stagingDir\lib\*.lib" -Destination "..\..\lib\Win32\Release" -Force
Copy-Item -Path "$stagingDir\bin\*.dll" -Destination "..\..\lib\Win32\Release" -Force
Set-Location -Path ".."

# 返回初始目录
Write-Host "返回上级目录..."
Set-Location -Path ".."

Write-Host "完成googletest库构建。"